﻿using ACC.Business;
using ACC.Business.Process;
using ACC.Business.Stn;
using ACC.Data;
using ACC.Data.ACCLog;
using ACC.Data.Comm;
using ACC.Data.Line;
using ACC.Device;
using ACC.Device.Client;
using ACC.Device.PLC.Siemens;
using ACC.Interface.Extensions;
using ACC.LineDevice.KukaPack;
using System;
using System.Data;
using System.Text;

namespace Script
{
    /// <summary>
    /// 模组烘烤
    /// </summary>
    public class L1M04010 : StnProcessKukaPack
    {
        public L1M04010(StnRunningData data)
            : base(data)
        {
        }

        protected override ACCDevice CreateDevice()
        {
            ACCDevice dev = base.CreateDevice();
            if (kukaSiemensDev != null)
            {
                kukaSiemensDev.DB190 = 800;
                kukaSiemensDev.DB190_Offset = 13000;
                kukaSiemensDev.DB192 = 80;
                kukaSiemensDev.DB192_PLC_Offset = 13066;
                kukaSiemensDev.DB193 = 800;
                kukaSiemensDev.DB193_PLC_Offset = 13034;
                kukaSiemensDev.DB193_ACC_Offset = 13068;
                kukaSiemensDev.DB194 = 800;
                kukaSiemensDev.DB194_PLC_Offset = 13034;
                kukaSiemensDev.DB194_PLC_Data_Offset = 13078;
            }

            return dev;
        }

        public override ACCCode SendLoadResult(ACCDevice dev, ACCDevice.LoadResult result)
        {
            if (result.ErrorCode == ACCCode.ACCOK)
            {
                SendBarcodeToPlc(new string[] { GetLotNo(_data.LastLoadUnit.UnitSN) }, kukaSiemensDev.DB190_Offset + 104, 30);//13104
            }
            return base.SendLoadResult(dev, result);
        }

        public override ACCCode ReadUnloadParam(ACCDevice dev, out ACCDevice.UnloadParam param)
        {
            ACCCode rs = base.ReadUnloadParam(dev, out param);
            param.UnitSN = PalletNo = GetUnLoadPallet();
            if (rs == ACCCode.ACCOK && !string.IsNullOrEmpty(param.UnitSN))
            {
                if (!(dev is ClientDevice))
                {
                    // 通过Pallet号获取内部码 
                    string[] v = ACCExt.GetPallet(this, param.UnitSN);
                    if (v != null && v.Length > 0)
                        param.UnitSN = v[0];
                }
            }

            // 确定是否需要换型
            WIPUnit unit = UnitDataProcess.GetWIPUnit(_lineData, param.UnitSN);
            if (unit != null)
            {
                rs = AutoChangePartNo(unit.PartNo, ref param);
            }

            return rs;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string GetUnLoadPallet()
        {
            string _pallet = "";
            try
            {
                byte[] data = new byte[32];
                byte[] temp = new byte[30];
                // TO-DO: +38 ??? 后面还有32个字节， 和DB193_ACC_Offset = 13068 是不是有冲突？
                ((SiemensDevice)_Device).ReadDBBytes(((SiemensDevice)_Device).DB194, kukaSiemensDev.DB194_PLC_Offset + 38, data);
                Array.Copy(data, 2, temp, 0, temp.Length);
                _pallet = Encoding.ASCII.GetString(temp);
                if (_pallet != null && _pallet.Length > data[1])
                {
                    _pallet = _pallet.Substring(0, data[1]);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.StackTrace);
            }
            return _pallet;
        }

        /// <summary>
        /// 通过模组内部码，先找关联的基体码，然后找基体码绑定电芯的批次号
        /// </summary>
        /// <param name="UNITSN"></param>
        /// <returns></returns>
        public string GetLotNo(string UNITSN)
        {
            string vs = "";
            string sql = string.Format("SELECT DISTINCT LOTNO FROM [ACC_BATTERY_DATA] WHERE BARCODE IN ( SELECT ID_VALUE FROM [ACC_UNITID]  WHERE LINE = 'PL02_JT' AND ID_NAME = '电芯' AND UNITSN IN (SELECT ID_VALUE FROM [DBO].[ACC_UNITID] WHERE   LINE = 'PL02_MZ' AND ID_NAME = '模组基体' AND UNITSN = '{0}'))",UNITSN);
            DataTable dt = DataManager.DBHelper.Query(sql);
            if (dt != null && dt.Rows.Count > 0)
            {
                vs = dt.Rows[0]["LOTNO"].ToString();
            }
            return vs;
        }
    }
}